// pages/clockStatis/clockStatis.js
const app = getApp()
const util = require('../../../../utils/util.js')
const lbbService = require('../../../service/service.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    members: [],
    index: 0,
    month: '',
    day: '01',
    dayIndex: 0,
    array: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this
    let date = that.getNowDate()
    that.setData({
      date: date,
      month: date.substr(0, 7),
      scrollHeight: app.getScrollHeight(50)
    })
    let num = parseInt(that.data.date.substr(8, 9))
    let days = ['整月']
    for (let i = 0; i < num; i++) {
      days.push(i + 1 + '号')
    }
    that.setData({
      days: days,
      dayIndex: days.length - 1
    })
    that.getMembers()
    that.getRecords()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },
  toDetail: function (e) {
    console.log('toDetail>>', e)
    let cardDate = e.currentTarget.dataset.date
    let uid = e.currentTarget.dataset.uid
    let type = e.currentTarget.dataset.type==1?1:2
    wx.navigateTo({
      url: '../clockDetail/clockDetail?cardDate=' + cardDate + '&uid=' + uid + '&type=' + type,
    })
  },
  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
    if (this.data.index == 0 && this.data.dayIndex == 0) {
      this.setData({
        dayIndex: this.data.days.length - 1
      })
    }
    this.getRecords()
  },
  bindDateChange: function(e) {
    let that = this
    console.log('bindDateChange>>', e.detail.value)
    let newMonth = e.detail.value
    let month = that.data.month
    let date = that.data.date
    that.setData({
      month: newMonth
    })
    if (month != newMonth) {
      that.getRecords()
      if (newMonth == date.substr(0, 7)) {
        let num = parseInt(date.substr(8, 9))
        let days = ['整月']
        for (let i = 0; i < num; i++) {
          days.push(i + 1 + '号')
        }
        that.setData({
          days: days
        })
      } else {
        let days = that.getDays(newMonth)
        that.setData({
          days: days
        })
      }
    }
  },
  bindDayChange: function(e) {
    let that = this
    console.log('bindDayChange>>', e.detail.value)
    that.setData({
      dayIndex: e.detail.value
    })
    let signRecords = that.data.signRecords
    if (parseInt(that.data.dayIndex) == 0) {
      console.log(that.data.index,'that.data.index',that.data.signRecords)
      if (that.data.index != 0 && that.data.index != "0") {
        that.setData({
          signRecordsFilter: signRecords
        })
        return
      } else {
        that.setData({
          index: 0
        })
        that.getRecords()
      }
    } else {
      if (that.data.index != 0 && that.data.index != "0") {
        signRecords = signRecords.filter(item => parseInt(item.cardDate.substr(8, 9)) == parseInt(that.data.days[that.data.dayIndex]))
        that.setData({
          signRecordsFilter: signRecords
        })
        return
      } else {
        that.getRecords()
      }
    }

  },
  getNowDate: function() {
    let reg = /\//g
    let nowTime = new Date()
    let nowDate = util.formatTime(nowTime).substr(0, 10).replace(reg, '-')
    return nowDate
  },
  // 获取考勤人员
  getMembers: function() {
    let that = this
    lbbService.getMembers({}, function(res) {
      let members = res.data
      let item = {}
      item.masterName = '全部'
      item.masterUid = ''
      members.push(item)
      that.setData({
        members: members.reverse()
      })
      that.setName()
    })
  },
  setName: function() {
    let that = this
    let members = that.data.members
    let array = []
    members.forEach(item => {
      array.push(item.masterName)
    })
    that.setData({
      array: array
    })
  },
  getRecords: function() {
    let that = this
    let uid = ''
    if (that.data.members.length > 0) {
      uid = that.data.members[that.data.index].masterUid
    }
    let cardDate = that.data.month
    console.log(cardDate,'cardDateA')
    if (that.data.index == 0 || that.data.index == "0") {
      uid = 0
      if(that.data.dayIndex > 0){
        cardDate = that.data.month + '-' + that.data.dayIndex;
      }
    }
    console.log(cardDate)
    lbbService.getAllCardInfo( { uid: uid, cardDate: cardDate }, function(res) {
      that.setData({
        signRecords: res.data.reverse(),
        signRecordsFilter: res.data,
      })
      if (that.data.index != 0 && that.data.dayIndex != 0) {
        let signRecords = that.data.signRecords.filter(item => parseInt(item.cardDate.substr(8, 9)) == parseInt(that.data.days[that.data.dayIndex]))
        that.setData({
          signRecordsFilter: signRecords
        })
      }
    })
  },
  getDays: function(date) {
    var d = new Date(parseInt(date.substr(0, 4)), parseInt(date.substr(5, 6)), 0)
    let num = d.getDate()
    let days = ['整月']
    for (let i = 0; i < num; i++) {
      days.push(i + 1 + '号')
    }
    return days
  }
})